﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Benefit.ViewModel.Day
{
    /// <summary>
    /// 当日交易明细
    /// </summary>
    public class TradeDetail
    {
        /// <summary>
        /// 左侧菜单显示信息
        /// </summary>
        public LeftMenuInfos LeftMenuInfos { get; set; }

        public Models.Sys.Operator Operator { get; set; }

        public DateTime Pdate { get; set; }

        public string HPdate { get; set; }

        public List<Models.Day.AccountDayMoney> AccountDayMoneys { get; set; }

        public Models.View.OperatorDayMoney OperatorDayMoney { get; set; }

        public List<Models.View.OperatorDayProductInfo> OperatorDayProductInfos { get; set; }

        public List<Models.View.OperatorDayPosition> OperatorDayPositions { get; set; }

        public string JS { get; set; }





        #region 定义一些使用到的接口
        private Interface.Sys.IOperator IOperator = null;
        private Interface.Sys.ITradeHistory ITradeHistory = null;
        private Interface.Sys.IDayOperatorAccount IDayOperatorAccount = null;
        private Interface.Day.IAccountDayMoney IAccountDayMoney = null;
        private Interface.View.IOperatorDayMoney IOperatorDayMoney = null;
        private Interface.View.IOperatorDayTick IOperatorDayTick = null;
        private Interface.View.IOperatorDayProductInfo IOperatorDayProductInfo = null;
        private Interface.View.IOperatorDayPosition IOperatorDayPosition = null;

        #endregion


        public TradeDetail(int uid, string queryDate, DB.DBManager db)
        {
            InitInterface(db);
            this.Operator = IOperator.GetModel(uid);

            string _year = queryDate.ToString().Substring(0, 4);
            string _month = queryDate.ToString().Substring(4, 2);
            string _day = queryDate.ToString().Substring(6, 2);
            string _pdate = _year + "-" + _month + "-" + _day;
            this.Pdate = Convert.ToDateTime(_pdate);
            this.HPdate = _pdate;

            Models.Sys.TradeHistory th = ITradeHistory.GetSettlementModel(queryDate);
            List<Models.Sys.Account> accounts = IDayOperatorAccount.GetDayOperatorAccounts(this.Pdate, this.Operator.Id);
            List<Models.Day.AccountDayMoney> accountMoneys = new List<Models.Day.AccountDayMoney>();
            foreach (Models.Sys.Account account in accounts)
            {
                Models.Day.AccountDayMoney m = new Models.Day.AccountDayMoney();
                m = IAccountDayMoney.GetAccountDayMoney(th.Id, account.Id);
                accountMoneys.Add(m);
            }
            this.AccountDayMoneys = accountMoneys;

            Models.View.OperatorDayMoney operatorMoney = IOperatorDayMoney.GetOperatorDayMoney(accountMoneys, this.Operator.Id, th.Id);
            if (operatorMoney != null)
            {
                this.OperatorDayMoney = operatorMoney;
            }
            else
            {
                this.OperatorDayMoney = new Models.View.OperatorDayMoney();
            }

            List<Models.View.OperatorDayTick> winTicks = IOperatorDayTick.GetOperatorDayWinOrLostTicks(this.Operator.Id, th.Id, true);
            List<Models.View.OperatorDayTick> lostTicks = IOperatorDayTick.GetOperatorDayWinOrLostTicks(this.Operator.Id, th.Id, false);

            this.OperatorDayProductInfos = IOperatorDayProductInfo.GetSortedOperatorDayProductInfo(th.Id, this.Operator.Id);
            this.OperatorDayPositions = IOperatorDayPosition.GetAllAccountDayPositionDelivery(th.Id, this.Operator.Id);

            this.JS += Benefit.Tools.ChartAtManager.ChartManager.TickPie(winTicks, "showflash4", "盈利Tick分布", "TickPie");
            this.JS += Benefit.Tools.ChartAtManager.ChartManager.TickPie(lostTicks, "showflash2", "亏损Tick分布", "TickPie2");
            DisposeInterface();
        }


        private void InitInterface(DB.DBManager db)
        {
            #region 初始化接口
            IOperator = new Service.Sys.ServiceOperator(db);
            ITradeHistory = new Service.Sys.ServiceTradeHistory(db);
            IDayOperatorAccount = new Service.Sys.ServiceDayOperatorAccount(db);
            IAccountDayMoney = new Service.Day.ServiceAccountDayMoney(db);
            IOperatorDayMoney = new Service.View.ServiceOperatorDayMoney(db);
            IOperatorDayTick = new Service.View.ServiceOperatorDayTick(db);
            IOperatorDayProductInfo = new Service.View.ServiceOperatorDayProductInfo(db);
            IOperatorDayPosition = new Service.View.ServiceOperatorDayPosition(db);

            #endregion
        }
        /// <summary>
        /// 释放接口
        /// </summary>
        private void DisposeInterface()
        {
            #region 释放接口
            IOperator = null;
            ITradeHistory = null;
            IDayOperatorAccount = null;
            IAccountDayMoney = null;
            IOperatorDayMoney = null;
            IOperatorDayTick = null;
            IOperatorDayProductInfo = null;
            IOperatorDayPosition = null;


            #endregion
        }
    }
}
